package gold.digger;

import java.util.*;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC429 {
    public long startExecuteTime = System.currentTimeMillis();


    class Node {
        public int val;
        public List<Node> children;

        public Node() {
        }

        public Node(int _val) {
            val = _val;
            children = new ArrayList<>();
        }

        public Node(int _val, List<Node> _children) {
            val = _val;
            children = _children;
        }
    }

    class Solution {
        public List<List<Integer>> levelOrder(Node root) {
            List<List<Integer>> res = new ArrayList<>();
            if (null == root) return res;

            Deque<Node> queue = new LinkedList();
            queue.offerLast(root);
            Node cur;

            while (!queue.isEmpty()) {
                int size = queue.size();
                List<Integer> curLayer = new ArrayList<>();
                for (int i = 0; i < size; i++) {
                    cur = queue.pollFirst();
                    curLayer.add(cur.val);
                    for (Node child : cur.children) {
                        queue.offerLast(child);
                    }
                }
                res.add(curLayer);
            }

            return res;
        }
    }

    public void run() {
        Solution solution = new Solution();
        Node n1 = new Node(1);
        Node n2 = new Node(3);
        Node n3 = new Node(2);
        Node n4 = new Node(4);
        Node n5 = new Node(5);
        Node n6 = new Node(6);
        n1.children.add(n2);
        n1.children.add(n3);
        n1.children.add(n4);

        n3.children.add(n5);
        n3.children.add(n6);

        System.out.println(solution.levelOrder(n1));
    }

    public static void main(String[] args) throws Exception {
        LC429 an = new LC429();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
